package com.one.group.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.mybatisflex.core.tenant.TenantManager;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.SpCellMapper;
import com.one.group.model.dto.SpCellUpdateDto;
import com.one.group.model.entity.SpBuilding;
import com.one.group.model.entity.SpCell;
import com.one.group.service.SpCellService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
public class SpCellServiceImpl extends ServiceImpl<SpCellMapper, SpCell> implements SpCellService {
    @Autowired
    private SpCellMapper spCellMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public boolean myUpdate(SpCellUpdateDto spCellUpdateDto) {
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        //获取原数据
        try {
            SpCell spCell = this.getById(spCellUpdateDto.getId());
            spCell.setName(spCellUpdateDto.getName());
            spCell.setExpirationTime(spCellUpdateDto.getExpirationTime());
            spCell.setAddress(spCellUpdateDto.getAddress());
            spCell.setBalance(spCellUpdateDto.getBalance());
            if(this.updateById(spCell)){
                rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
                rabbitTemplate.convertAndSend("gxa.direct","deleteCell",spCellUpdateDto.getId());
                rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
                rabbitTemplate.convertAndSend("gxa.direct","cellUpdate",tenantId);
                return true;
            }else return false;
        } catch (Exception e) {
            return false;
        }

    }
    @Override
    public boolean myRemoveById(Long id) {
        Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
        try {
            if(this.removeById(id)){
                rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
                rabbitTemplate.convertAndSend("gxa.direct","deleteCell",id);
                rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
                rabbitTemplate.convertAndSend("gxa.direct","cellUpdate",tenantId);
                return true;
            }else return false;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public List<Long> searchBuildingIdList(String cellName) {
        try {
            //获取当前租户id
            Long tenant_id = Convert.toLong(StpUtil.getExtra("tenant_id"));
            return spCellMapper.searchBuildingIdListByCellName(cellName, tenant_id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public SpCell myGetById(Long id) {
        //配置忽略多租户
        TenantManager.ignoreTenantCondition();
        return this.getById(id);
    }

    @Override
    public String getAllCell() {
        return getMapper().getAllCell().toString();
    }
}
